package cc.cc4414.cc02.chat.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cc.cc4414.cc02.chat.entity.GroupUser;
import cc.cc4414.cc02.chat.service.GroupUserService;
import cc.cc4414.cc02.core.controller.BaseController;
import cc.cc4414.cc02.core.result.ResultVO;

/**
 * @author cc 2018年8月17日
 */
@RestController
@RequestMapping("/groupUser")
public class GroupUserController extends BaseController {

	@Autowired
	private GroupUserService groupUserService;

	/**
	 * 将用户userId加入群groupId
	 * 
	 * @param groupId
	 *            群组id
	 * @param userId
	 *            用户id
	 * @return 是否添加成功
	 */
	@PostMapping("add")
	public ResultVO<Object> add(@RequestParam Long groupId, @RequestParam Long userId) {
		groupUserService.add(groupId, userId, getUser().getId());
		return new ResultVO<>();
	}

	/**
	 * 用户离开群组
	 * 
	 * @param groupId
	 *            群组id
	 * @return 是否离开成功
	 */
	@PostMapping("delete")
	public ResultVO<Object> delete(@RequestParam Long groupId) {
		groupUserService.delete(groupId, getUser().getId());
		return new ResultVO<>();
	}

	/**
	 * 查询自己所在的所有群组
	 * 
	 * @return 查询到的数据
	 */
	@GetMapping("findGroupUsers")
	public ResultVO<List<GroupUser>> findGroupUsers() {
		return new ResultVO<>(groupUserService.findGroupUsers(getUser().getId()));
	}

	/**
	 * 查询群组里面的群成员
	 * 
	 * @param groupId
	 *            群组id
	 * @return 查询到的数据
	 */
	@GetMapping("findGroupMember")
	public ResultVO<List<GroupUser>> findGroupMember(@RequestParam Long groupId) {
		return new ResultVO<>(groupUserService.findGroupMember(groupId, getUser().getId()));
	}

}
